<?php
/**
 * Created by PhpStorm.
 * User: caicm
 * Date: 2017/6/15
 * Time: 12:00
 */

namespace WMApi\Model;


use Think\Model;

class UsersModel extends Model {

    /**
     * [客户端]检查用户等级
     * 
     * @param number $user_id
     * 
     * @author ^2_3^
     */
    public function check_user_rank( $user_id = 0 ) {
        $flag = true;
        
        if( empty($user_id) ) {
            returnApiError('check_user_rank()缺少user_id参数;');
        }
        
        /// 用户信息
        $where_u = array(
            "user_id" => $user_id
        );
        $user_info = $this->where($where_u)->find();
        
        /// 用户余额<=0，会员等级降为普通会员
        if( $user_info['user_rank'] != 1 && $user_info['user_money'] <= 0 ) {
            $this->startTrans();
            
            $flag_u = false;
            $flag_ua = false;
            
            /// (1)、更新用户等级(users)[降为普通会员]
            $data_u = array(
                "user_rank" => 1
            );
            $flag_u = $this->where($where_u)->save( $data_u );
            
            /// (2)、插入会员变更记录(user_action)[降为普通会员]
            //-/ 上次变更时间
            $where_ua = array(
                "user_id" => $user_id,
                "action_type" => "升至VIP"
            );
            $action_time_last = M('user_action')->where($where_ua)->order("action_time desc")->getField("action_time");
            if( empty($action_time_last) ) {
                $action_time_last = '-';
            }else {
                $action_time_last = local_date('Y-m-d H:i:s', $action_time_last);
            }
            //-/ 插入变更记录
            $data_ua = array(
                "action_name" => "buyer",
                "action_time" => gmtime(),
                "action_type" => "降至注册用户",
                "old_value" => $action_time_last,
                "user_id" => $user_id,
                "new_value" => date('Y-m-d H:i:s')
            );
            $flag_ua = M('user_action')->data($data_ua)->add();
            
            if( $flag_u && $flag_ua ) {
                $this->commit();
                
            }else {
                $this->rollback();
                $flag = false;
            }
        }
        
        return $flag;
    }
    
    /**
     * 获取用户收货地址信息
     * @param int $uid
     * @param int $address_id
     */
    public function get_user_address_info($uid, $address_id) {
        $where_u = array();
        if( empty($uid) ) {
            returnApiError('提示:get_user_address_info参数错误;');
        }else {
            $where_u['u.user_id'] = $uid;
        }
        
        $where_ua = '';
        if( !empty($address_id) ) {
            $where_ua .= " AND ua.address_id = {$address_id} ";
        }
        
        $user_info = $this->alias(u)
            ->field('
                    u.user_id AS uid,
                	u.user_name,
                	ua.province,
                	rp.region_name AS province_name,
                	ua.city,
                	rc.region_name AS city_name,
                	ua.district,
                	rd.region_name AS district_name,
                	ua.address,
                	ua.address_name,
                	ua.sign_building,
                	ua.consignee,
                	ua.mobile,
                	ua.longitude,
                	ua.latitude
                ')
                ->join("sdx_user_address ua ON u.user_id = ua.user_id {$where_ua}", 'left')
                ->join('sdx_region rp ON rp.region_id = ua.province', 'left')
                ->join('sdx_region rc ON rc.region_id = ua.city', 'left')
                ->join('sdx_region rd ON rd.region_id = ua.district', 'left')
                ->where( $where_u )
                ->find();
// w_log('cyb_log', $this->getLastSql());
        
        return $user_info;
    }
    
    /**
     * 根据手机号码获取会员表数量
     * Created By caicm
     * @param int $phone
     * @param int $platform
     * @return mixed
     * *
     */
    public function get_users_conut_by_phone($phone=0, $platform=1){

        $where = array(
            'mobile_phone' => $phone,
            'platform' => $platform
        );

        return $this->where($where)->count('user_id');

    }


    /**
     * 获取商家信息
     * Created By caicm
     * @param int $seller_id
     * @param int $user_id
     * @param int $phone
     * @param int $platform
     * @return mixed
     */
    public function find_users_seller_info($seller_id=0, $user_id=0, $phone=0, $platform=1){

        $where = array(
            'u.mobile_phone' => $phone,
            's.seller_id' => $seller_id,
            'u.user_id' => $user_id,
            'platform' => $platform
        );
        if($phone==0) unset($where['u.mobile_phone']);
        if($seller_id==0) unset($where['s.seller_id']);
        if($user_id==0) unset($where['u.user_id']);

        $field = 'u.ec_salt,u.mobile_phone,u.password,u.user_id,s.seller_id,s.seller_token';
        $join = 'left join sdx_wm_seller as s on u.user_id=s.user_id';

        return $this->alias('u')->field($field)->join($join)->where($where)->find();

    }

    /**
     * 写入users表（商家注册）
     * Created By caicm
     * @param string $username
     * @param string $password
     * @param string $email
     * @param string $ec_salt
     * @param int $mobile_phone
     * @return mixed
     */
    public function add_users($username='', $password='', $email='', $ec_salt='', $mobile_phone=0){

        $data = array(
            'user_name' => $username,
            'password' => $password,
            'email' => $email,
            'mobile_phone' => $mobile_phone,
            'is_validated' =>1,
            'user_rank' => 1,
            'reg_time' => gmtime(),
            'last_login' => gmtime(),
            'is_hd' => get_is_hd(),
            'ec_salt' => $ec_salt,
            'platform' => 1
        );

        return M("users")->add($data);

    }

    /**
     * 修改登录密码
     * Created By caicm
     * @param int $phone
     * @param string $password
     * @param string $ec_salt
     * @param int $platform
     * @return bool
     */
    public function update_password($phone=0, $password='', $ec_salt='', $platform=1){

        $where = array(
            'mobile_phone' => $phone,
            'platform' => $platform
        );

        $data = array(
            'password' => $password,
            'ec_salt' => $ec_salt
        );

        return $this->where($where)->data($data)->save();

    }

    /**
     * 修改手机号码
     * Created By caicm
     * @param int $user_id
     * @param int $mobile_phone
     * @return bool
     */
    public function update_phone($user_id=0, $mobile_phone=0){
        $where = array(
            'user_id' => $user_id
        );

        $data = array(
            'mobile_phone' => $mobile_phone,
        );

        return $this->where($where)->data($data)->save();
    }

    /**
     * 写入 用户记录
     * Author:何锐 <poptser@foxmail.com>
     */
    public function write_user_action($name,$user_id,$type,$old_value,$new_value){
        $data['action_name'] = $name;       //操作者用户名
        $data['action_time'] = gmtime();    //操作时间
        $data['action_type'] = $type;       //操作类型
        $data['old_value'] = $old_value;    //原来值
        $data['new_value'] = $new_value;    //新值
        $data['user_id'] = $user_id;        //用户ID
        M('user_action')->add($data);
    }

}